<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class EShop extends FrontEndController {
    public function __construct() {
        parent::__construct();
        $this->load->library('mypaging');
        $this->load->model('repo/shop_model', 'shop_model');
    }

    public function intialPaging(){
        $this->mypaging->setCount($this->shop_model->countShop());
        $this->mypaging->setLimit(SHOP_LIMIT);
        $this->mypaging->setMainURL(shop_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex=1) {
        $this->intialPaging();
        $offset = ($pageIndex - 1) * SHOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aShop = $this->shop_model->paging(SHOP_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'EShop',
            'aShop' => $aShop,
            'pagingBar' => $pagingBar,
            );
        $this->renderView('pages/e-shop', $aReturn);
    }

    private function countShopByCategory($cate_id, $cate_slug){
        $this->mypaging->setCount($this->shop_model->countShopByCategory($cate_id));
        $this->mypaging->setLimit(SHOP_LIMIT);
        $this->mypaging->setMainURL(shop_cate_paging_url($cate_slug));
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function getShopByCategory($slug, $pageIndex=1){
        $cate = $this->cate_model->getCateBySlug($slug);
        if($cate==null){
            show_404();
        }
        $this->countShopByCategory($cate->getId(), $slug);
        $offset = ($pageIndex - 1) * SHOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aShop = $this->shop_model->getShopByCategory($cate->getId(), array(), SHOP_LIMIT, $offset);
        $aReturn = array(
            'pageTitle' => 'EShop',
            'aShop' => $aShop,
            'pagingBar' => $pagingBar,
            );
        $this->renderView('pages/e-shop', $aReturn);
    }

    public function countShopForCategory(){
        $countShop = array();
        $aItems = $this->input->post('item');
        if($aItems!==false && count($aItems)>0) {
            foreach($aItems as $key=>&$value){
                $value = $this->encrypt->decode($value);
            }
            $countShop = $this->shop_model->countShopByCategory($aItems);
            var_dump($countShop);
        }
        $this->renderJSON($countShop);
        die();
    }
}
?>